package com.zjw.zy.coreui.fragment

import com.zjw.zy.base.BaseFragment
import android.os.Bundle
import android.view.View
import com.zjw.zy.R
import com.zjw.zy.coreui.view.ImageViewContent
import com.zjw.zy.ext.back
import com.zjw.zy.ext.mainActivity
import com.zjw.zy.utils.PathUtils
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.WindowUtils
import kotlin.math.max
import kotlin.streams.toList

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/27 13:32
 * @see com.zjw.zy.coreui.fragment.ImageContentFragment
 */
class ImagePreviewFragment : BaseFragment() {


    companion object {
        //图片源目录
        const val IMAGE_ROOT_PATH = "IMAGE_ROOT_PATH"

        //正使用的路径
        const val IMAGE_PREVIEW_POSITION = "IMAGE_PREVIEW_POSITION"


        const val UI_MODE_LIGHT = "UI_MODE_LIGHT"
        const val UI_MODE_NIGHT = "UI_MODE_NIGHT"
    }

    private val imageContent: ImageViewContent by lazy { findViewById(R.id.image_preview_content) }

    private var mCurrentUiMode = UI_MODE_LIGHT

    override fun onPageEnter() {
        WindowUtils.TopBar.showBack()
        WindowUtils.TopBar.hideAllRightButton()
        WindowUtils.FootBar.hide()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {}
    override fun onSubscribe() {}
    override fun onBindViewListener() {
        imageContent.bindListener(object : ImageViewContent.Listener {
            override fun onSingleTapUp() {
                processUiMode()
            }

            override fun onLongPress() {
            }
        })
    }

    private fun processUiMode() {
        if (mCurrentUiMode == UI_MODE_LIGHT) {
            mCurrentUiMode = UI_MODE_NIGHT
        } else {
            mCurrentUiMode = UI_MODE_LIGHT
        }
    }

    override fun onBindViewValue() {
        arguments?.getString(IMAGE_ROOT_PATH)?.let {
            val selectPath = arguments?.getString(IMAGE_PREVIEW_POSITION)
            val data = PathUtils.getImageInfoListFromPath(it)
            val position = data.stream().map { image -> image.path }.toList().indexOf(selectPath)
            imageContent.bindData(data, max(0, position))
        }

    }

    override fun onFindView(rootView: View) {}
    override fun onLayoutId(): Int {
        return R.layout.image_preview_fragment
    }

    override fun onPageExit() {}
}